package org.cmg.study.spring.controller;

import org.cmg.study.spring.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author CMG
 * @version 1.0
 * @cLassName ModelAttributeController
 * @description
 * @date 2021/9/3 16:57
 **/
@Controller
public class ModelAttributeController {
    @ModelAttribute
    public void myModel(@RequestParam(required = false) String name, Model model){
        model.addAttribute("name", name);
    }

    @RequestMapping("/model")
    public String model(){
        return "test";
    }

    @RequestMapping("/model1")
    public String mdel1(@RequestParam(required = false) String name, Model model){
        model.addAttribute("name", name);
        return "test";
    }

    @ModelAttribute("name")
    public String myModel2(@RequestParam(required = false) String name){
        return name;
    }

    @RequestMapping("/model2")
    public String model2(){
        return "test1";
    }

    @RequestMapping("/register")
    public String register(@ModelAttribute("user") User user) {
        if ("xg".equals(user.getName()) && "12345".equals(user.getPwd())) {
            return "login";
        } else {
            return "register";
        }
    }

    // @ModelAttribute和@RequestMapping同时放在方法上
    @RequestMapping(value = "/index")
    @ModelAttribute("name")
    public String model(@RequestParam(required = false) String name) {
        return name;
    }
}
